﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DatabaseCore;
using Eaktest;
using SystemCore;

namespace EakTest
{
    public partial class Login : Form
    {
        IUserDao userDao = new UserDao();

        public Login()
        {
            InitializeComponent();

            txtUsername.Focus();

            txtUsername.Text = "admin";
            txtPassword.Text = "1234";
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtUsername.Text.Trim();
            string password = txtPassword.Text.Trim();

            if (string.IsNullOrEmpty(userName))
            {
                MessageDialog.Information("Please enter a username.");
                return;
            }

            if (string.IsNullOrEmpty(password))
            {
                MessageDialog.Information("Please enter a password.");
                return;
            }

            tbl_User user = userDao.Login(userName, password);
            if (user == null)
            {
                MessageDialog.Information("Can't login");
                txtUsername.Focus();
                return;
            }

            new UserContext(user);

            new MenuForm().ShowDialog();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnLogin.PerformClick();
            }
        }
        
    }
}
